﻿using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ERP.Web.Controllers.ProductionSystem
{
    public class PendingtasksController : Controller
    {
        private readonly IPendingtasksService _pendingtasksService;

        public PendingtasksController(IPendingtasksService pendingtasksService)
        {
            _pendingtasksService = pendingtasksService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 待检任务查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult PendingtasksQuery()
        {
            return View();
        }



        /// <summary>
        /// 获取待检任务分页列表（layui表格专用）
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <param name="formCode">单据编码</param>
        /// <param name="materialName">物资名称</param>
        /// <param name="testingItems">检测类型</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPendingtasksPageList(int page = 1, int limit = 10, string? formCode = null, string? materialName = null, string? testingItems = null)
        {
            try
            {
                var query = new PendingtasksQueryDTO
                {
                    Page = page,
                    Limit = limit,
                    FormCode = formCode,
                    MaterialName = materialName,
                    TestingItems = testingItems
                };

                var result = await _pendingtasksService.GetPendingtasksPageList(query);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new PendingtasksPageResultDTO
                {
                    Code = 1,
                    Msg = $"查询失败：{ex.Message}",
                    Count = 0,
                    Data = new List<PendingtasksDTO>()
                });
            }
        }

        /// <summary>
        /// 获取待检任务列表
        /// </summary>
        /// <param name="formCode">单据编码</param>
        /// <param name="materialName">物资名称</param>
        /// <param name="testingItems">检测类型</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PendingtasksModel>> GetPendingtasksList(string? formCode, string? materialName, string? testingItems)
        {
            var pendingtasksList = await _pendingtasksService.GetPendingtasksList(formCode, materialName, testingItems);
            if (pendingtasksList == null)
            {
                throw new Exception("获取待检任务信息失败");
            }
            return pendingtasksList;
        }

        /// <summary>
        /// 添加过程检验记录
        /// </summary>
        /// <param name="processDto">过程检验数据</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddProcess([FromBody] ProcessDTO processDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { success = false, message = "数据验证失败" });
                }

                var result = await _pendingtasksService.AddProcess(processDto);
                if (result)
                {
                    return Json(new { success = true, message = "过程检验记录添加成功" });
                }
                else
                {
                    return Json(new { success = false, message = "过程检验记录添加失败" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = $"添加失败：{ex.Message}" });
            }
        }
    }
}
